Skip to content

gh-151722: Defer GC tracking in frozendict.copy()#152230

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:frozendict_copy
Jun 26, 2026
Merged

gh-151722: Defer GC tracking in frozendict.copy()#152230
vstinner merged 2 commits into
python:mainfrom
vstinner:frozendict_copy

Conversation

@vstinner

@vstinner vstinner commented Jun 25, 2026

Copy link
Copy Markdown
Member

Fix _PyDict_Or() and frozendict.copy(): only track the frozendict by the GC once the dictionary is fully initialized.

Functions modifying frozendict now ensures that the object is not tracked by the GC (in debug mode).

  • can_modify_dict() checks that _PyObject_GC_IS_TRACKED() is false for frozendicts.
  • dict_merge_api() makes sure that the dictionary is tracked by the GC.

Fix _PyDict_Or() and frozendict.copy(): only track the frozendict by
the GC once the dictionary is fully initialized.

Functions modifying frozendict now ensures that the object is not
tracked by the GC (in debug mode).

* can_modify_dict() checks that _PyObject_GC_IS_TRACKED() is false
  for frozendicts.
* dict_merge_api() makes sure that the dictionary is tracked by the
  GC.
@vstinner

Copy link
Copy Markdown
Member Author

@corona10 @methane: More frozendict fixes.

With this change, there are now many checks in can_modify_dict() to make sure that we don't modify a frozendict exposed in Python. It should catch future bugs.

@corona10

Copy link
Copy Markdown
Member

Let me take a look till this weekend.

@corona10 corona10 self-requested a review June 25, 2026 20:03

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm lgtm

@vstinner vstinner merged commit 05679f3 into python:main Jun 26, 2026
55 checks passed
@vstinner vstinner deleted the frozendict_copy branch June 26, 2026 11:55
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for the review.

@bedevere-app

bedevere-app Bot commented Jun 26, 2026

Copy link
Copy Markdown

GH-152271 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 26, 2026
vstinner added a commit that referenced this pull request Jun 26, 2026
…152271)

gh-151722: Defer GC tracking in frozendict.copy() (GH-152230)

Fix _PyDict_Or() and frozendict.copy(): only track the frozendict by
the GC once the dictionary is fully initialized.

Functions modifying frozendict now ensures that the object is not
tracked by the GC (in debug mode).

* can_modify_dict() checks that _PyObject_GC_IS_TRACKED() is false
  for frozendicts.
* dict_merge_api() makes sure that the dictionary is tracked by the
  GC.
(cherry picked from commit 05679f3)

Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants